<?php

namespace app\services;

use app\models\Provinces;
use app\models\Cities;
use app\models\Areas;

class CommonService extends ServiceBase
{
    public function getProvince()
    {
        return Provinces::select();
    }

    public function getCities($provinceId)
    {
        $where = [];
        if ($provinceId) {
            $where = ['provinceId' => $provinceId];
        }

        return Cities::where($where)->select();
    }

    public function getAreas($cityId)
    {
        $where = [];
        if ($cityId) {
            $where = ['cityId' => $cityId];
        }

        return Areas::where($where)->select();
    }

    /**
     * @param $provinceName
     * @return Provinces|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProvinceByName($provinceName)
    {
        $res= Provinces::where("provinceName","like","%".$provinceName."%")->find();

        return $res;
    }

    /**
     * @param $cityName
     * @param string $provinceId
     * @return Cities|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCityByName($cityName, $provinceId = '')
    {
        $city = Cities::where("cityName","like","%".$cityName."%");

        if ($provinceId) {
            $city->where('provinceId', '=', $provinceId);
        }

        return $city->find();
    }

    /**
     * @param $areaName
     * @param string $cityId
     * @return Areas|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAreaByName($areaName, $cityId = '')
    {
        $area = Areas::where("areaName","like","%".$areaName."%");

        if ($cityId) {
            $area->where('cityId', '=', $cityId);
        }

        return $area->find();
    }

}